<template>
  <div class="login-container">
      <el-form
          class="login-form"
          :rules="rules"
          ref="form"
          :model="user"
          size="large"
          @submit.prevent="handleSubmit"
      >
          <div class="login-form__header">
              <h2>登录</h2>
          </div>
          <el-form-item prop="account">
              <el-input
                  v-model="user.account"
                  placeholder="请输入用户名"
              >
                  <template #prefix>
                      <i class="el-input__icon el-icon-user" />
                  </template>
              </el-input>
          </el-form-item>
          <el-form-item prop="pwd">
              <el-input
                  v-model="user.pwd"
                  type="password"
                  placeholder="请输入密码"
              >
                  <template #prefix>
                      <i class="el-input__icon el-icon-lock" />
                  </template>
              </el-input>
          </el-form-item>
          <el-form-item>
              <el-button
                  class="submit-button"
                  type="primary"
                  :loading="loading"
                  native-type="submit"
              >
                  登录
              </el-button>
          </el-form-item>
      </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import type { IElForm, IFormRule } from '@/types/element-plus'
import { store } from '@/store'

const router = useRouter()
const route = useRoute()

const user = reactive({
  account: 'admin',
  pwd: '123456',
  imgcode: '',
})
const loading = ref(false)
// key 属性名都是 字符串, 值 是 数组
const rules = ref<IFormRule>({
  account: [
    { required: true, message: '请输入账号', trigger: 'change' }
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'change' }
  ],
  // imgcode: [
  //   { required: true, message: '请输入验证码', trigger: 'change' }
  // ]
})
// const captchaSrc = ref('')
const form = ref<IElForm | null>(null)

onMounted(() => {
  // loadCaptcha()
})

// const loadCaptcha = async () => { // 获取图片验证码
//   const data = await getCaptcha()
//   captchaSrc.value = URL.createObjectURL(data)
// }

const handleSubmit = async () => {
  // 登录请求
  // 表单验证
  const valid = await form.value?.validate()
  if (!valid) {
    return false
  }
  // 验证通过, 展示loading
  // loading.value = true
  // 请求提交
  // const data = await login(user).finally(() => {
  // loading.value = false
  // })
  store.commit('setUser', {
      // ...data.user_info,
      username:"ShaoYuan",password:"NFiUAtyvVXkd73npwuk59badS4DfGFkUrRZ/Bs5/G8+n2CUCafLkQDPODTq+DkhuvZPIZ0sG+F8vhDAsUkZb1A==",
      time: 1702873329155,
      // token: data.token
      token: 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM1ZjNhNTNkLTYyZWMtNGI0ZC04ZGJlLTc4NDkwZWVjYjg3ZCJ9.xqYcoUN6vOcaIHcqkZg6Lj8WXVhnORaVp1swWP7E7Uiut5v0fCP9r4tP7iviSyl0vDdSdxQgf8fjZC7TJB9n7g',
  })
  // 获取当前路由对象
  let redirect = route.query.redirect || '/'
  if (typeof redirect !== 'string') {
      redirect = '/'
  }
  router.replace(redirect)
}
</script>

<style lang="scss" scoped>
.login-container {
  min-width: 400px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
}

.login-form {
  padding: 30px;
  border-radius: 6px;
  background: #fff;
  min-width: 350px;
  .login-form__header {
      display: flex;
      justify-content: center;
      align-items: center;
      padding-bottom: 30px;
  }

  .el-form-item:last-child {
      margin-bottom: 0;
  }

  .login__form-title {
      display: flex;
      justify-content: center;
      color: #fff;
  }

  .submit-button {
      width: 100%;
  }
}
</style>
